package com.by.controller;

import cn.heimi.dto.OrderQueryDTO;
import cn.heimi.dto.OrderingDTO;
import cn.heimi.dto.OrderingOkDTO;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/order")
public class OrderController {

    @GetMapping("/test")
    public String test() {
        return "order-service--->test";
    }
    @RequestMapping("/ordering")
    public String getOrder() {
        return "下单成功";
    }

    @RequestMapping("/ordering2")
    public OrderingOkDTO ordering2() {
        OrderingOkDTO orderingOk = OrderingOkDTO.builder().id(10000L).needDay(new BigDecimal(100)).build();
        return orderingOk;
    }
    @PostMapping("/ordering3")
    public OrderingOkDTO ordering3(@RequestBody OrderingDTO orderingDTO) {
        List<String> collect = orderingDTO.getProducts().stream().map(item -> item.getName()).collect(Collectors.toList());
        String msg = StrUtil.join(",", collect);
        OrderingOkDTO orderingOk = OrderingOkDTO.builder().id(10000L).msg(msg).needDay(new BigDecimal(100)).build();
        return orderingOk;
    }

    @GetMapping ("/select")
    public String select(@SpringQueryMap OrderQueryDTO orderQueryDTO) {
      String msg = StrUtil.EMPTY;
      if (orderQueryDTO.getId() != null) {
          msg = "我是通过id查询过来的：id" + orderQueryDTO.getId();
      }
      if (orderQueryDTO.getUserId() != null) {
          msg = "我是通过userid查询过来的:userId" + orderQueryDTO.getUserId();
      }
        return msg ;
    }


    @GetMapping("/sleep")
    public String sleep(@RequestParam Integer time) {
        ThreadUtil.safeSleep(time*1000);
        String msg = "我睡眠了"+time+"秒";
        return msg;
    }
}
